var current_day_one;
var current_day_two;
var current_day_three ;
var current_day ;
var hb;
var tb;
var current_month ;
var time;

//var basePath = "http://127.0.0.1:80/GraduationProject";

/**
 * 
 */
$(function(){
	getAnalysisData(userId);
	
	$("#restoreBtn").bind("click",function(){
		$("#detailDataTable").hide();
    	$("#main").show();
	});

});

/**
 * 获取数据分析
 */
function getAnalysisData(userId){
	current_day_one = [];
	current_day_two = [];
	current_day_three = [];
	current_day = [];
	hb = [];
	tb = [];
	current_month = [];
	time = [];
	
	$.ajax({
		type:"get",
		url:basePath + "/distributor/Income/" + userId,
		async:true,
		success:function(data){
			if(data.status == "200"){
				
				for(var i in data.obj){
					for(var j in data.obj[i]){
						if(j == "current_day_one"){
							current_day_one.push(data.obj[i][j]);
						}else if(j == "current_day_two"){
							current_day_two.push(data.obj[i][j]);
						}else if(j == "current_day_three"){
							current_day_three.push(data.obj[i][j]);
						}else if(j == "current_day"){
							current_day.push(data.obj[i][j]);
						}else if(j == "current_month"){
							current_month.push(data.obj[i][j]);
						}else if(j == "op_time"){
							time.push(data.obj[i][j]);
						}
					}
				}
				
				loadChartData();
			}
		}
	});
}


/**
 * 图表数据
 */
function loadChartData(){
	 // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));
    var myChart2 = echarts.init(document.getElementById('main2'));
    // 指定图表的配置项和数据
    var option =  {
    	    backgroundColor: '#FFFFFF',
    	    title: {
    	        //text: '检查企业信息分析'
    	    },
    	    tooltip: {
    	        trigger: 'axis',
    	        axisPointer: { // 坐标轴指示器，坐标轴触发有效
    	            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
    	        }
    	    },
    	    toolbox: {
    	        feature: {
    	           /* dataView: {
    	                show: true,
    	                readOnly: false
    	            },*/
    	            myTool2:{//自定义按钮 danielinbiti,这里增加，selfbuttons可以随便取名字    
	                    show:true,//是否显示    
	                    title:'查看明细', //鼠标移动上去显示的文字    
	                    icon:'path://M525.4 721.2H330.9c-9 0-18.5-7.7-18.5-18.1V311c0-9 9.3-18.1 18.5-18.1h336.6c9.3 0 18.5 9.1 18.5 18.1v232.7c0 6 8.8 12.1 15 12.1 6.2 0 15-6 15-12.1V311c0-25.6-25.3-48.9-50.1-48.9h-335c-26.2 0-50.1 23.3-50.1 48.9v389.1c0 36.3 20 51.5 50.1 51.5h197.6c6.2 0 9.3-7.5 9.3-15.1 0-6-6.2-15.3-12.4-15.3zM378.8 580.6c-6.2 0-12.3 8.6-12.3 14.6s6.2 14.6 12.3 14.6h141.4c6.2 0 12.3-5.8 12.3-13.4 0.3-9.5-6.2-15.9-12.3-15.9H378.8z m251.6-91.2c0-6-6.2-14.6-12.3-14.6H375.7c-6.2 0-12.4 8.6-12.4 14.6s6.2 14.6 12.4 14.6h240.8c6.2 0.1 13.9-8.5 13.9-14.6z m-9.2-120.5H378.8c-6.2 0-12.3 8.6-12.3 14.6s6.2 14.6 12.3 14.6h240.8c7.7 0 13.9-8.6 13.9-14.6s-6.2-14.6-12.3-14.6z m119.4 376.6L709 714.1c9.2-12 14.6-27 14.6-43.2 0-39.4-32.1-71.4-71.8-71.4-39.7 0-71.8 32-71.8 71.4s32.1 71.4 71.8 71.4c16.3 0 31.3-5.4 43.4-14.5l31.6 31.5c3.8 3.8 10 3.8 13.8 0 3.8-3.8 3.8-10 0-13.8z m-88.8-23.6c-28.3 0-51.3-22.8-51.3-51s23-51 51.3-51c28.3 0 51.3 22.8 51.3 51s-23 51-51.3 51z', //图标    
	                    // icon:'查看明细',
	                    onclick:function() {//点击事件,这里的option1是chart的option信息    
	                    	loadDetailData();
	                    }    
	                },
	                restore: {
    	                show: true
    	            },
    	            saveAsImage: {
    	                show: true
    	            },
    	        }
    	    },
    	    legend: {
    	    	 data: ['当日一级收入', '当日二级收入', '当日三级收入'],
    	    	 selected: {  
                     '当日三级收入' : true
                     //不想显示的都设置成false  
                 }  
    	    },
    	    grid: {
    	        left: '3%',
    	        right: '4%',
    	        bottom: '3%',
    	        containLabel: true
    	    },
    	    xAxis: [{
    	        type: 'category',
    	        data: time
    	    }],
    	    yAxis: [{
    	        type: 'value',
    	        axisLabel: {
    	            formatter: '{value}'
    	        }
    	    }],
    	    series: [{
    	        name: '当日一级收入',
    	        type: 'bar',
    	        itemStyle: {
    	            normal: {
    	                color: '#01949B'
    	            },
    	        },
    	        markPoint: {
    	            data: [{
    	                    type: 'max',
    	                    name: '最大值'
    	                },
    	                {
    	                    type: 'min',
    	                    name: '最小值'
    	                }
    	            ]
    	        },
    	        markLine: {
    	            data: [{
    	                type: 'average',
    	                name: '平均值'
    	            }]
    	        },
    	        data: current_day_one
    	    }, {
    	        name: '当日二级收入',
    	        type: 'bar',
    	        itemStyle: {
    	            normal: {
    	                color: '#EBA954'
    	            },
    	        },
    	        markPoint: {
    	            data: [{
    	                    type: 'max',
    	                    name: '最大值'
    	                },
    	                {
    	                    type: 'min',
    	                    name: '最小值'
    	                }
    	            ]
    	        },
    	        markLine: {
    	            data: [{
    	                type: 'average',
    	                name: '平均值'
    	            }]
    	        },
    	        data: current_day_two
    	    }, {
    	        name: '当日三级收入',
    	        type: 'bar',
    	        itemStyle: {
    	            normal: {
    	                color: '#C23531'
    	            },
    	        },
    	        markPoint: {
    	            data: [{
    	                    type: 'max',
    	                    name: '最大值'
    	                },
    	                {
    	                    type: 'min',
    	                    name: '最小值'
    	                }
    	            ]
    	        },
    	        markLine: {
    	            data: [{
    	                type: 'average',
    	                name: '平均值'
    	            }]
    	        },
    	        data: current_day_three
    	    }]
    	};
    
    var option2 = {
    	    backgroundColor: '#FFFFFF',
    	    title: {
    	        text: '日（月）收入分析'
    	    },
    	    tooltip: {
    	        trigger: 'axis'
    	    },
    	    legend: {
    	        data: ['本日', '本月']
    	    },
    	    grid: {
    	        left: '3%',
    	        right: '4%',
    	        bottom: '3%',
    	        containLabel: true
    	    },
    	    toolbox: {
    	        feature: {
    	            saveAsImage: {}
    	        }
    	    },
    	    xAxis: {
    	        type: 'category',
    	        boundaryGap: false,
    	        data: time
    	    },
    	    yAxis: {
    	        type: 'value',
    	        name: '本日/本月',
    	        min:0,
    	        max: 13000,
    	        interval: 5000,
    	    },
    	    series: [{
    	            name: '本日',
    	            type: 'line',

    	            stack: '总量',
    	            data: current_day

    	        },
    	        {
    	            name: '本月',
    	            type: 'line',
    	            stack: '总量',
    	            data:current_month

    	        }
    	    ]
    	};

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
    myChart2.setOption(option2);
}

/**
 * 获取详细数据
 */
function loadDetailData(){
	$("#detailDataTable").show();
	$("#main").hide();
	$.ajax({
		type:"get",
		url:basePath + "/distributor/lowers",
		async:false,
		success:function(data){
			if(data){
				console.log(data);
				for(var i in data.obj){
					if(data.obj[i].level == "1"){
						data.obj[i].commission = data.obj[i].current_day * 0.15 ;
					}else if(data.obj[i].level == "2"){
						data.obj[i].commission = data.obj[i].current_day * 0.11 ;
					}else if(data.obj[i].level == "3"){
						data.obj[i].commission = data.obj[i].current_day * 0.08 ;
					}
				}
				console.log(data.obj);
				$('#detailData').bootstrapTable("load",data.obj);
			}
		}
	});
}

/**
 * 获取当前用户信息
 */
function getUserInfo(){
	var currentUser;
	$.ajax({
		type:"get",
		url:basePath + "/user/detail",
		async:false,
		success:function(data){
			if(data){
				currentUser = data.obj;
				getAnalysisData(currentUser.id);
			}
		}
	});
	return currentUser;
}




